/* Set up the log*/
cd $ohie
cap log close
global sysdate: disp %tdYYNNDD  date("`c(current_date)'", "DMY")
qui log using 	"./logs/linmte_no_covars_graph_data_$sysdate.log", replace

* Set up timer
timer clear 1
timer on 1 

/*----------------------------------------------------------------------*/
/* PROGRAM: linmte_no_covars_graph_data.do				*/
/*									*/
/* PURPOSE:								*/
/* [*]	This code outputs the data necessary for graphing the MTE 	*/
/*	bounds and the linear MTE without covariates (also called	*/
/*	MTE(p)). The data is used in the following exhibits of the 	*/
/*	paper (this code does not create the exhibits. It just creates 	*/
/*	the data needed to support the exhibits):	 		*/
/*	- averages_Y_num_eqs						*/
/*	- identified_outcomes						*/
/*									*/
/* OUTPUT:								*/
/* [*]	linmte_no_covars_graph_data: This output file 			*/
/*	contains the data necessary for replicating the above-mentioned	*/
/*	exhibits of the paper.						*/
/* NOTES:								*/
/* [*]	This code does not compute treatment effects and does not 	*/
/*	bootstrap the MTE components.					*/
/*									*/
/*----------------------------------------------------------------------*/

* Set up display options
clear
set type double
set more off, permanently

*********************************************
* VARIABLES AND CONTROLS		    
*********************************************

* Input data set	
local indata oregonnumhh1

* List of outcomes
local Ys	"Y_num"

local Y_num_lab "Number of ER Visits"


* Endogenous variable
local D		"any_medicaid"

local D_lab	"Medicaid"

* Instrument
local Z		"Z"
		
*********************************************
* OUTPUT TO EXCEL FUNCTION	    	*
*********************************************
* This is the setup for outputting the data we need for graphing into Excel 	
capture program drop output_stats

program define output_stats
	args varlabel value column row tabname
	quietly{
		/* Export to Excel*/
		version 14.1: putexcel set ///
			"$output/linmte_no_covars_graph_data.xls", ///
			sheet("`tabname'") modify
		global `row' = $`row' + 1
		version 14.1: putexcel A1=("_NAME_") `column'1=("VALUE")
		version 14.1: putexcel A$`row'=("`varlabel'") `column'$`row'=(`value')
	}
end

*********************************************
* RUN CODE			    	*
*********************************************	

* Begin looping through outcomes
foreach file in `indata' {
foreach Y of local Ys{

	use "$final/`file'.dta", clear
	
	* For each outcome drop individuals with missing values
	* Before estimating the MTE we always drop individuals with missing 
	* values for the outcome. We include this step for reproducibility 
	* purposes (to be able to replicate the same MTE(p) across codes)
	keep if `Y' != . 
	
	* Output to log
	di "Calculating the linear MTE without covariates for `Y'"
			
*********************************************
* ESTIMATE THE MTE(p) COMPONENTS		    
*********************************************
			
	* Compute BTTO
	* BTTO = E[Y|D=1, Z=0]
	quietly su `Y' if `D' == 1 & `Z' == 0
	local BTTO = `r(mean)'
	
	* Compute ITTO
	* ITTO = E[Y|D=1,Z=1]
	quietly su `Y' if `D' == 1 & `Z' == 1 
	local ITTO = `r(mean)'
	
	* Compute IUUO
	* IUUO = E[Y|D=0, Z=1]
	quietly su `Y' if `D' == 0 & `Z' == 1 
	local IUUO = `r(mean)'
				
	* Compute BUUO
	* BUUO = E[Y|D=0, Z=0]
	quietly su `Y' if `D' == 0 & `Z' == 0 
	local BUUO = `r(mean)'	
		
	* Compute pB
	* pB = P(D=1|Z=0)
	quietly su `D' if `Z'==0 
	local pB = `r(mean)'
		
	* Compute pI
	* pI = P(D=1|Z=1)
	quietly su `D' if `Z'==1 
	local pI = `r(mean)'
		
	* Compute LAUO
	* We compute it using the closed form expression reported in the paper			
	local LAUO = (1/(`pI' - `pB'))*((1-`pB')*`BUUO' - (1-`pI')*`IUUO')
		
	* Compute LATO
	* We compute it using the closed form expression reported in the paper	
	local LATO = (1/(`pI' - `pB'))*(`pI'*`ITTO' - `pB'*`BTTO')
		
			
			
*********************************************
* DATA FOR GRAPHING		    
*********************************************
		
	* Initialize the iterator for outputting to Excel
	* This is absolutely necessary for the output function to work
	global exceliter = 1
	
	* BTTO 
	output_stats BTTO `BTTO' B exceliter `Y'_`file'
		
	* LATO
	output_stats LATO `LATO' B exceliter `Y'_`file'
		
	* LAUO
	output_stats LAUO `LAUO' B exceliter `Y'_`file'
		
	* IUUO
	output_stats IUUO `IUUO' B exceliter `Y'_`file'
	
	* ITTO
	output_stats ITTO `ITTO' B exceliter `Y'_`file'
	
	* BUUO
	output_stats BUUO `BUUO' B exceliter `Y'_`file'
	
	*pB
	output_stats pB `pB' B exceliter `Y'_`file'
	
	*pI
	output_stats pI `pI' B exceliter `Y'_`file'
	
	
	* Export graph data to a csv for the TiKz files
	mat MTE_`Y'_`file' = J(1,8,.)
	mat coln MTE_`Y'_`file' = pB pI BTTO LATO LAUO IUUO ITTO BUUO
		
	mat MTE_`Y'_`file'[1,1] = `pB'
	mat MTE_`Y'_`file'[1,2] = `pI'
	mat MTE_`Y'_`file'[1,3] = `BTTO'
	mat MTE_`Y'_`file'[1,4] = `LATO'
	mat MTE_`Y'_`file'[1,5] = `LAUO'
	mat MTE_`Y'_`file'[1,6] = `IUUO'
	mat MTE_`Y'_`file'[1,7] = `ITTO'
	mat MTE_`Y'_`file'[1,8] = `BUUO'



	* Export to csv
	preserve
	clear
	svmat MTE_`Y'_`file', n(col)
	g Dlab = `""`D_lab'""'
	g Ylab = `""``Y'_lab'""'

	export delimited using "$output/linmte_no_covars_`Y'_`file'.csv", replace
	restore
		
} // close loop that iterates through variables
} // close file loop
	
timer off 1
timer list 1
local hours = `r(t1)'/3600
di "Computing time is `hours' hours"
	
qui log close
